package com.cfuture08.eweb4j.jdbc.sql;

import com.cfuture08.eweb4j.cache.ORMConfigBeanCache;
import com.cfuture08.eweb4j.orm.config.bean.ORMConfigBean;
import com.cfuture08.eweb4j.orm.config.bean.Property;
import com.cfuture08.util.ReflectUtil;
import com.cfuture08.util.StringUtil;

/**
 * 生成删除语句
 * 
 * @author cfuture.wg
 * @since v1.a.0
 */
public class DeleteSqlCreator<T> {
	private T[] ts;

	public DeleteSqlCreator() {
	}

	public DeleteSqlCreator(T... ts) {
		this.ts = ts;
	}

	public String delete(String condition) {
		if (this.ts != null && this.ts.length > 0) {
			StringBuilder sb = new StringBuilder();
			for (T t : this.ts) {
				sb.append(this.makeSQL(t, condition));
			}
			return sb.toString();
		} else {
			return "";
		}
	}

	private String makeSQL(T t, String condition) {
		ORMConfigBean ormBean = ORMConfigBeanCache.get(t.getClass());
		String table = ormBean != null ? ormBean.getTable() : t.getClass()
				.getSimpleName();
		return String.format("DELETE FROM %s WHERE %s ;", table, condition);
	}

	public String[] delete() throws Exception {
		String[] sqls = new String[ts.length];
		for (int i = 0; i < ts.length; i++) {
			sqls[i] = this.makeSQL(ts[i]);
		}
		return sqls;
	}

	public String[] delete(String[] columns, String[] values) throws Exception {
		String[] sqls = new String[ts.length];
		for (int i = 0; i < ts.length; i++) {
			sqls[i] = this.makeSQL(ts[i], columns, values);
		}
		return sqls;
	}

	public String[] delete(String[] columns) throws Exception {
		String[] sqls = new String[ts.length];
		for (int i = 0; i < ts.length; i++) {
			sqls[i] = this.makeSQL(ts[i], columns);
		}
		return sqls;
	}

	private String makeSQL(T t) throws Exception {
		// 从缓存中取出配置信息
		ORMConfigBean ormBean = ORMConfigBeanCache.get(t.getClass());
		String table = ormBean == null ? t.getClass().getSimpleName() : ormBean
				.getTable();
		StringBuilder condition = new StringBuilder();
		ReflectUtil ru = new ReflectUtil(t);
		if (ormBean == null) {
			// 已经有主键存在，则组合。
			if (condition.length() > 0) {
				condition.append(" AND ");
			}
			condition.append(" id = ");
			Object value = ru.getMethod("getId").invoke(t);
			condition.append("'" + value + "'");

		} else {
			for (Property property : ormBean.getProperty()) {
				if ("true".equals(property.getPk())
						|| "1".equals(property.getPk())) {
					// 已经有主键存在，则组合。
					if (condition.length() > 0) {
						condition.append(" AND ");
					}

					condition.append(property.getColumn() + " = ");

					Object value = ru.getMethod(
							"get"
									+ StringUtil.toUpCaseFirst(property
											.getName())).invoke(t);

					condition.append("'" + value + "'");
					break;
				}
			}
		}

		return String.format("DELETE FROM %s WHERE %s ;", table, condition);
	}

	private String makeSQL(T t, String[] columns, String[] values)
			throws Exception {
		// 从缓存中取出配置信息
		ORMConfigBean ormBean = ORMConfigBeanCache.get(t.getClass());
		String table = ormBean == null ? t.getClass().getSimpleName() : ormBean
				.getTable();
		StringBuilder condition = new StringBuilder();
		for (int i = 0; i < columns.length; ++i) {
			// 已经有主键存在，则组合。
			if (condition.length() > 0) {
				condition.append(" AND ");
			}
			condition.append(columns[i] + " = ");
			condition.append("'" + values[i] + "'");

		}
		return String.format("DELETE FROM %s WHERE %s ;", table, condition);
	}

	private String makeSQL(T t, String[] columns) throws Exception {
		// 从缓存中取出配置信息
		ORMConfigBean ormBean = ORMConfigBeanCache.get(t.getClass());
		String table = ormBean == null ? t.getClass().getSimpleName() : ormBean
				.getTable();
		StringBuilder condition = new StringBuilder();
		ReflectUtil ru = new ReflectUtil(t);
		if (ormBean == null) {
			for (String column : columns) {
				if (ru.getField(column) != null) {
					if (condition.length() > 0) {
						condition.append(" AND ");
					}
					condition.append(column + " = ");
					Object value = ru.getMethod(
							"get" + StringUtil.toUpCaseFirst(column)).invoke(t);
					condition.append("'" + value + "'");
				}

			}
		} else {
			for (Property property : ormBean.getProperty()) {
				for (String column : columns) {
					if (column.equals(property.getColumn())) {
						if (condition.length() > 0) {
							condition.append(" AND ");
						}
						condition.append(property.getColumn() + " = ");
						Object value = ru.getMethod(
								"get"
										+ StringUtil.toUpCaseFirst(property
												.getName())).invoke(t);
						condition.append("'" + value + "'");

					}
				}
			}
		}
		return String.format("DELETE FROM %s WHERE %s ;", table, condition);
	}

	public T[] getTs() {
		return ts;
	}

	public void setTs(T[] ts) {
		this.ts = ts;
	}
}
